<?php
namespace app\index\controller;

use app\index\model\BidModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowLogModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowSetModel;
use app\index\model\FlowShareModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\TaskModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\service\FlowService;
use app\index\service\OptService;
use app\index\service\UserService;
use app\index\service\WorkerService;
use app\index\service\XingService;
use think\Db;
use think\Exception;
use think\Request;

class Daily extends Common
{

    public function list_bill_create(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bill.summary|bill.enginame','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name')
            ->join('user user','user.id=bill.created_user')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        $data['is_modeid'] = isset($param['modeid']) ? $param['modeid'] : '';
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bill_hand(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','=', 0];

        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name')
            ->join('user user','user.id=bill.created_user')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bill_finish(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','in', [1,2,3,5]];

        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name')
            ->join('user user','user.id=bill.created_user')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_bill_turn(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 0];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bill.summary|bill.enginame|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = FlowBillModel::alias('bill')
            ->field('bill.*,user.realname as created_user_name')
            ->join('user user','user.id=bill.created_user')
            ->where($where)
            ->order('bill.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();


        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function del_bill_turn(Request $request){
        $param = $request->post();
        $data = FlowBillModel::where(['id'=>$param['id']])->find();
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        if($data['created_user'] != $this->userinfo['id']){
            return outputJson(4000,lang('code_4000'));
        }
        if($data['isturn'] != 0){
            return outputJson(4000,lang('code_4000'));
        }


        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            Db::name($data['modenum'])->where(['id'=>$data['mid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    public function list_bill_share(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];

        $whereShare = "";
        if(isset($param['types']) && !empty($param['types'])){
            if($param['types'] == 'tome'){
                $whereShare = Db::raw("find_in_set(".$this->userinfo['id'].",domer)");
            }elseif($param['types'] == 'meto'){
                $whereShare = Db::raw("share.uid=".$this->userinfo['id']);
            }else{
                $whereShare = Db::raw("share.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            }
        }else{
            $whereShare = Db::raw("share.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
        }

        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['created_at_share']) && !empty($param['created_at_share'])){
            $seadt = explode(' - ', $param['created_at_share']);
            $where[] = ['share.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['fenname']) && !empty($param['fenname'])){
            $where[] = ['fen.realname','like', '%'.$param['fenname'].'%'];
        }


        $geiWhere = '';
        if(isset($param['geiname']) && !empty($param['geiname'])){
            $getId = UserModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where('realname','like','%'.$param['geiname'].'%')->column('id');
            foreach ($getId as $gid){
                if(empty($geiWhere)){
                    $geiWhere = "find_in_set(".$gid.",domer)";
                }else{
                    $geiWhere .= " or find_in_set(".$gid.",domer)";
                }
            }
            if(empty($geiWhere)){
                $where[] = ['share.id','lt', 0];
            }
        }


        $data['data'] = FlowShareModel::alias('share')
            ->field('bill.*,share.uid,share.domer,share.id as shareid,share.created_at as created_at_share
            ,user.realname as created_user_name')
            ->leftJoin('flow_bill bill','share.billid = bill.id')
            ->leftJoin('user user','user.id=bill.created_user')
            ->leftJoin('user fen','fen.id=share.uid')
            ->where($where)
            ->where($geiWhere)
            ->where($whereShare)
            ->order('share.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['uid_name'] = UserModel::get_realname_by_pk($item['uid']);
                $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();

        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_add_bill(Request $request){
        $param = $request->post();
        $where = [];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }

        $whereTeshu = "";
        if($this->userinfo['token_roletype'] == 1 && in_array(25, explode(',', $this->userinfo['roleid']))){
            $whereTeshu = "id in (2,58,61,3,12,55,59)";
        }

        $info = [];
        $set = FlowSetModel::where($where)->where($whereTeshu)->order('sort ASC')->select()->toArray();
        foreach ($set as $key=>&$item){
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$item['id']])->find();
            if(!$exist){
                $insid = FlowClassModel::insertGetId(['comid'=>$this->userinfo['comid'], 'setid'=>$item['id'],]);
                $exist = FlowClassModel::where(['id'=>$insid])->find();
            }
            $item['id'] = $exist['id'];
            $item['status'] = $exist['status'];
            if(isset($param['status']) && $param['status'] != ''){
                if($param['status'] != $item['status']){
                    unset($set[$key]);
                    continue;
                }
            }

            $item['ispublish'] = false;
            $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$item['id'],'status'=>1,'deleted_user'=>0])->find();
            if($course){
                $item['ispublish'] = true;
                $step = json_decode($course['step'], true);
                if($step && is_array($step)){
                    if((new FlowService())->check_course_start_users($step)){
                        $info[] = $item;
                    }
                }
            }
        }

        $list = [];
        $nav = (new UserService())->get_flowset_nav();
        foreach ($nav as $k=>&$v){
            $children = [];
            foreach ($info as $vv){
                if($vv['typename'] == $k){
                    $children[] = $vv;
                }
            }
            if($children){
                $list[] = ['name'=>$v, 'children'=>$children];
            }
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_add_bill_mob(Request $request){
        $param = $request->post();
        $where = [];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }


        $info = [];
        $set = FlowSetModel::where($where)->order('sort ASC')->select()->toArray();
        foreach ($set as $key=>&$item){
            $exist = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$item['id']])->find();
            if(!$exist){
                $insid = FlowClassModel::insertGetId(['comid'=>$this->userinfo['comid'], 'setid'=>$item['id']]);
                $exist = FlowClassModel::where(['id'=>$insid])->find();
            }
            $item['modeid'] = $item['id'];
            $item['id'] = $exist['id'];
            $item['status'] = $exist['status'];
            if(isset($param['status']) && $param['status'] != ''){
                if($param['status'] != $item['status']){
                    unset($set[$key]);
                    continue;
                }
            }

            $item['ispublish'] = false;
            $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$item['id'],'status'=>1,'deleted_user'=>0])->find();
            if($course){
                $item['ispublish'] = true;
                $step = json_decode($course['step'], true);
                if($step && is_array($step)){
                    if((new FlowService())->check_course_start_users($step)){
                        $info[] = $item;
                    }
                }
            }
        }

        $list = [];

        //最近发起的
        $jin = FlowBillModel::field('modeid')->where(['deleted_user'=>0,'created_user'=>$this->userinfo['id']])
            ->group('modeid')->order(Db::raw("MAX(created_at) DESC"))->select()->toArray();
        $jinArr = [];
        foreach ($jin as $vvv){
            foreach ($info as $setvv){
                if($vvv['modeid'] == $setvv['modeid']){
                    $jinArr[] = $setvv;
                }
            }
        }
        $list[] = ['name'=>'最近', 'children'=>$jinArr];

        $nav = (new UserService())->get_flowset_nav();
        foreach ($nav as $k=>&$v){
            $children = [];
            foreach ($info as $vv){
                if($vv['typename'] == $k){
                    $children[] = $vv;
                }
            }
            if($children){
                $list[] = ['name'=>$v, 'children'=>$children];
            }
        }
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_flow_hand(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','=', 0];
        $where[] = ['task.deleted_user','=', 0];
        $where[] = ['task.uid','=', $this->userinfo['id']];
        $where[] = ['task.state','=', 1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bill.summary|bill.enginame|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['created_at_song']) && !empty($param['created_at_song'])){
            $seadt = explode(' - ', $param['created_at_song']);
            $where[] = ['task.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }

        $data['data'] = FlowTaskModel::alias('task')
            ->field('bill.*,task.created_at as created_at_song,user.realname as created_user_name')
            ->join('flow_bill bill','bill.id=task.billid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->where($where)
            ->order('task.created_at DESC')
            ->group('bill.id')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();



        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_finish(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['shen.uid','=', $this->userinfo['id']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bill.summary|bill.enginame|user.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at_song']) && !empty($param['created_at_song'])){
            $seadt = explode(' - ', $param['created_at_song']);
            $where[] = ['shen.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['updated_at_shen']) && !empty($param['updated_at_shen'])){
            $seadt = explode(' - ', $param['updated_at_shen']);
            $where[] = ['shen.updated_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }

        $data['data'] = FlowTaskShenModel::alias('shen')
            ->field('bill.*,user.realname as created_user_name')
            ->join('flow_bill bill','bill.id=shen.billid')
            ->leftJoin('user user','user.id=bill.created_user')
            ->where($where)
            ->order('shen.id DESC')
            ->group('bill.id')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                $shen = FlowTaskShenModel::where(['billid'=>$item['id'],'uid'=>$this->userinfo['id']])->order('id DESC')->find();
                $item['shenrecall'] = $shen['recall'];
                $item['created_at_song'] = $shen['created_at'];
                $item['updated_at_shen'] = $shen['updated_at'];
                $item['shenname'] = $shen['name'];

                $item['shenrecall'] = 0;
                if($item['isturn'] == 1 && $item['status'] == 0){
                    $taskShen = FlowTaskShenModel::where(['billid'=>$item['id'],'uid'=>$this->userinfo['id'],'recall'=>1])
                        ->where('opt','in',['add','zhuan','agree','tui','next'])->order('id DESC')->find();
                    if($taskShen){
                        $item['shenrecall'] = 1;
                    }
                }

                return $item;
            })->toArray();



        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function list_flow_promer(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
//        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];

        $wherePromer = "";
        if(isset($param['types']) && !empty($param['types'])){
            if($param['types'] == 'tome'){
                $wherePromer = Db::raw("find_in_set(".$this->userinfo['id'].",domer)");
            }elseif($param['types'] == 'meto'){
                $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']);
            }else{
                $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            }
        }else{
            $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
        }

        if(isset($param['fenname']) && !empty($param['fenname'])){
            $where[] = ['fen.realname','like', '%'.$param['fenname'].'%'];
        }


        $geiWhere = '';
        if(isset($param['geiname']) && !empty($param['geiname'])){
            $getId = UserModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where('realname','like','%'.$param['geiname'].'%')->column('id');
            foreach ($getId as $gid){
                if(empty($geiWhere)){
                    $geiWhere = "find_in_set(".$gid.",domer)";
                }else{
                    $geiWhere .= " or find_in_set(".$gid.",domer)";
                }
            }
            if(empty($geiWhere)){
                $where[] = ['promer.id','lt', 0];
            }
        }


        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['user.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bill.status','=', $param['status']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }
        if(isset($param['created_at_promer']) && !empty($param['created_at_promer'])){
            $seadt = explode(' - ', $param['created_at_promer']);
            $where[] = ['promer.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }

        $data['data'] = FlowPromerModel::alias('promer')
            ->field('bill.*,promer.uid,promer.domer,promer.id as promerid,promer.created_at as created_at_promer
            ,user.realname as created_user_name')
            ->join('flow_bill bill','promer.billid = bill.id')
            ->join('user user','user.id=bill.created_user')
            ->join('user fen','fen.id=promer.uid')
            ->where($where)
            ->where($wherePromer)
            ->where($geiWhere)
            ->order('promer.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['ische'] = $item['uid'] == $this->userinfo['id'] ? true : false;
                $item['uid_name'] = UserModel::get_realname_by_pk($item['uid']);
                $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);
                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
                return $item;
            })->toArray();

        $data['statusArr'] = (new FlowService())->get_flow_status();
        $data['is_status'] = isset($param['status']) ? $param['status'] : '';
        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function list_todo(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['uid','=',$this->userinfo['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name|mess','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['mess']) && !empty($param['mess'])){
            $where[] = ['mess','like', '%'.$param['mess'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['status','=', $param['status']];
        }

        if(isset($param['tododt']) && !empty($param['tododt'])){
            $seadt = explode(' - ', $param['tododt']);
            $where[] = ['tododt','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }


        $data['data'] = TodoModel::where($where)
            ->order('id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function allread_todo(Request $request){
        if($request->isPost()){
            $param = $request->post();

            Db::startTrans();
            try {
                TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->update([
                    'status' => 1,
                    'readdt' => date('Y-m-d H:i:s')
                ]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(2000,lang('code_2000'));
    }




    public function list_task(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['t.deleted_user','=', 0];
        $where[] = ['t.comid','=',$this->userinfo['comid']];
        $where[] = ['t.created_user','=',$this->userinfo['id']];


        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['t.status','=', $param['status']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['t.number|t.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['t.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['t.name','like', '%'.$param['name'].'%'];
        }

        $whererZhi = "";
        if(isset($param['zhiname']) && !empty($param['zhiname'])){
            $zhiidArr = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where('realname','like','%'.$param['zhiname'].'%')
                ->column('id');
            foreach ($zhiidArr as $vv){
                if(empty($whererZhi)){
                    $whererZhi = " FIND_IN_SET(".$vv.",`t`.`zhier`)";
                }else{
                    $whererZhi = " or FIND_IN_SET(".$vv.",`t`.`zhier`)";
                }
            }
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['t.type','=', $param['type']];
        }
        if(isset($param['youxian']) && !empty($param['youxian'])){
            $where[] = ['t.youxian','=', $param['youxian']];
        }

        if(isset($param['planstartdt']) && !empty($param['planstartdt'])){
            $planstartdt = explode(' - ', $param['planstartdt']);
            $where[] = ['t.planstartdt','between', [$planstartdt[0], $planstartdt[1]]];
        }
        if(isset($param['planenddt']) && !empty($param['planenddt'])){
            $planenddt = explode(' - ', $param['planenddt']);
            $where[] = ['t.planenddt','between', [$planenddt[0], $planenddt[1]]];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $enddt = explode(' - ', $param['enddt']);
            $where[] = ['t.enddt','between', [$enddt[0], $enddt[1]]];
        }


        $data['youxian'] = (new XingService())->get_task_youxian();
        $data['is_youxian'] = isset($param['youxian']) ? $param['youxian'] : '';

        $data['type'] = (new XingService())->get_task_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';


        $data['data'] = TaskModel::alias('t')
            ->field('t.*,user.realname as created_user_name,bid.name as bidname,e.name as enginname')
            ->leftJoin('bid bid','bid.id=t.bidid')
            ->leftJoin('engin e','e.id=t.enginid')
            ->leftJoin('user user','user.id=t.created_user')
            ->where($where)
            ->where($whererZhi)
            ->order('t.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['enginname'] = $item['bidname'];
                }
                if($item['type'] == 3){
                    $item['enginname'] = '';
                }
                $item['status_showname'] = (new XingService())->get_task_status_show($item['status']);
                $item['status_name'] = (new XingService())->get_task_status_list($item['status']);
                $item['type_name'] = (new XingService())->get_task_type_show($item['type']);
                $item['zhier_name'] = UserModel::get_realname_by_ids($item['zhier']);
                return $item;
            })->toArray();


        $nav = [];
        $status = (new XingService())->get_task_status();
        $allnums = 0;
        foreach ($status as $k=>$v){
            $num = TaskModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where(['created_user'=>$this->userinfo['id']])->where(['status'=>$k])->count();
            $allnums += $num;
            $nav[] = ['id'=>$k, 'title'=>$v.'（'.$num.'）'];
        }
        array_unshift($nav, ['id'=>0, 'title'=>'全部（'.$allnums.'）']);
        $data['nav'] = $nav;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_taskzhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['t.deleted_user','=', 0];
        $where[] = ['t.comid','=',$this->userinfo['comid']];
        $whereZhiMy = Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`t`.`zhier`)");


        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['t.status','=', $param['status']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['t.number|t.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['t.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['t.name','like', '%'.$param['name'].'%'];
        }

        $whererZhi = "";
        if(isset($param['zhiname']) && !empty($param['zhiname'])){
            $zhiidArr = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where('realname','like','%'.$param['zhiname'].'%')
                ->column('id');
            foreach ($zhiidArr as $vv){
                if(empty($whererZhi)){
                    $whererZhi = " FIND_IN_SET(".$vv.",`t`.`zhier`)";
                }else{
                    $whererZhi = " or FIND_IN_SET(".$vv.",`t`.`zhier`)";
                }
            }
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['t.type','=', $param['type']];
        }
        if(isset($param['youxian']) && !empty($param['youxian'])){
            $where[] = ['t.youxian','=', $param['youxian']];
        }

        if(isset($param['planstartdt']) && !empty($param['planstartdt'])){
            $planstartdt = explode(' - ', $param['planstartdt']);
            $where[] = ['t.planstartdt','between', [$planstartdt[0], $planstartdt[1]]];
        }
        if(isset($param['planenddt']) && !empty($param['planenddt'])){
            $planenddt = explode(' - ', $param['planenddt']);
            $where[] = ['t.planenddt','between', [$planenddt[0], $planenddt[1]]];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $enddt = explode(' - ', $param['enddt']);
            $where[] = ['t.enddt','between', [$enddt[0], $enddt[1]]];
        }


        $data['youxian'] = (new XingService())->get_task_youxian();
        $data['is_youxian'] = isset($param['youxian']) ? $param['youxian'] : '';

        $data['type'] = (new XingService())->get_task_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';


        $data['data'] = TaskModel::alias('t')
            ->field('t.*,user.realname as created_user_name,bid.name as bidname,e.name as enginname')
            ->leftJoin('bid bid','bid.id=t.bidid')
            ->leftJoin('engin e','e.id=t.enginid')
            ->leftJoin('user user','user.id=t.created_user')
            ->where($where)
            ->where($whereZhiMy)
            ->where($whererZhi)
            ->order('t.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['type'] == 1){
                    $item['enginname'] = $item['bidname'];
                }
                if($item['type'] == 3){
                    $item['enginname'] = '';
                }
                $item['status_showname'] = (new XingService())->get_task_status_show($item['status']);
                $item['status_name'] = (new XingService())->get_task_status_list($item['status']);
                $item['type_name'] = (new XingService())->get_task_type_show($item['type']);
                $item['zhier_name'] = UserModel::get_realname_by_ids($item['zhier']);
                return $item;
            })->toArray();


        $nav = [];
        $status = (new XingService())->get_task_status();
        $allnums = 0;
        foreach ($status as $k=>$v){
            $num = TaskModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`zhier`)"))->where(['status'=>$k])->count();
            $allnums += $num;
            $nav[] = ['id'=>$k, 'title'=>$v.'（'.$num.'）'];
        }
        array_unshift($nav, ['id'=>0, 'title'=>'全部（'.$allnums.'）']);
        $data['nav'] = $nav;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_task(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入任务名称');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择任务类别');
            }else{
                if($param['type'] == 1){
                    if(empty($param['bidid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                    $param['enginid'] = 0;
                }elseif($param['type'] == 2){
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                    $param['bidid'] = 0;
                }else{
                    $param['bidid'] = 0;
                    $param['enginid'] = 0;
                }
            }
            if(empty($param['zhier'])){
                return outputJson(4000, '请选择执行人');
            }
            if(empty($param['youxian'])){
                return outputJson(4000, '请选择优先级');
            }



            $param['number'] = (new XingService())->create_task_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = TaskModel::insertGetId($param);
                (new OptService())->relation_file($files,'task',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'task', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'task', $id, '任务');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new XingService())->create_task_number();
            $data['youxian'] = (new XingService())->get_task_youxian();
            $data['type'] = (new XingService())->get_task_type();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_task(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = TaskModel::where(['id'=>$param['id']])->find();

            if(empty($param['name'])){
                return outputJson(4000, '请输入任务名称');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择任务类别');
            }else{
                if($param['type'] == 1){
                    if(empty($param['bidid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                    $param['enginid'] = 0;
                }elseif($param['type'] == 2){
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择项目名称');
                    }
                    $param['bidid'] = 0;
                }else{
                    $param['bidid'] = 0;
                    $param['enginid'] = 0;
                }
            }
            if(empty($param['zhier'])){
                return outputJson(4000, '请选择执行人');
            }
            if(empty($param['youxian'])){
                return outputJson(4000, '请选择优先级');
            }


            if(empty($param['startdt'])){
                if(!empty($param['enddt'])){
                    if($param['enddt'] > date("Y-m-d H:i:s")){
                        return outputJson(4000, '实际完成时间不能大于当前时间');
                    }
                }
            }else{

                if($param['startdt'] > date("Y-m-d H:i:s")){
                    return outputJson(4000, '实际开始时间不能大于当前时间');
                }
                if(!empty($param['enddt'])){
                    if($param['enddt'] > date("Y-m-d H:i:s")){
                        return outputJson(4000, '实际完成时间不能大于当前时间');
                    }
                    if($param['startdt'] > $param['enddt']){
                        return outputJson(4000, '实际开始时间不能大于实际完成时间');
                    }
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            $editStr = '';
            if($param['zhier'] != $old['zhier']){
                $zhierName = UserModel::get_realname_by_ids($param['zhier']);
                $editStr .= '执行人修改为：'.$zhierName.'；';
            }
            if($param['planenddt'] != $old['planenddt']){
                $editStr .= '计划完成时间修改为：'.$param['planenddt'].'；';
            }
            if($param['startdt'] != $old['startdt']){
                $editStr .= '实际开始时间修改为：'.$param['startdt'].'；';
            }
            if($param['enddt'] != $old['enddt']){
                $editStr .= '实际完成时间修改为：'.$param['enddt'].'；';
            }
            if($param['rate'] != $old['rate']){
                $editStr .= '完成比例修改为：'.$param['rate'].'%；';
            }



            if(!empty($param['enddt'])){
                $param['status'] = 5;
            }else{
                if(!empty($param['startdt'])){
                    $param['status'] = 3;
                }else{
                    $param['status'] = 1;
                }
            }


            Db::startTrans();
            try {
                TaskModel::update($param);
                (new OptService())->relation_file($files,'task',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'task', $param['id'], '',$editStr);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'task', $param['id'], '任务');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TaskModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = [];
            if($data['data']['type'] == 1){
                $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
            }elseif($data['data']['type'] == 2){
                $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            }
            $data['engininfo'] = $engininfo;

            $data['data']['zhier_info'] = (new UserService())->get_users_part_arr($data['data']['zhier'], 'user');

            $data['youxian'] = (new XingService())->get_task_youxian();
            $data['type'] = (new XingService())->get_task_type();
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['files'] = (new OptService())->get_file('task', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_task(Request $request){
        $param = $request->post();
        $data = TaskModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            TaskModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'task', $data['id'], '任务');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_task(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = TaskModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $engininfo = [];
        if($data['data']['type'] == 1){
            $engininfo = BidModel::get_info_by_pk($data['data']['bidid']);
        }elseif($data['data']['type'] == 2){
            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        }
        $data['engininfo'] = $engininfo;

        $data['data']['status_name'] = (new XingService())->get_task_status_show($data['data']['status']);
        $data['data']['type_name'] = (new XingService())->get_task_type_show($data['data']['type']);
        $data['data']['zhier_name'] = UserModel::get_realname_by_ids($data['data']['zhier']);
        $data['files'] = (new OptService())->get_file('task', $data['data']['id']);
        $data['data']['flowrelas_info'] = FlowBillModel::field('id,sericnum,modename,modenum,mid')
            ->where('id','in',$data['data']['flowrelas'])->select()->toArray();
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function close_task(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = TaskModel::where(['id'=>$param['id']])->find();

            if(empty($param['explainclose'])){
                return outputJson(4000, '请输入关闭说明');
            }

            $param['status'] = 7;
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);




            $editStr = '关闭说明：'.$param['explainclose'].'；';



            Db::startTrans();
            try {
                TaskModel::update($param);


                $mess = '任务关闭：';
                $mess .= (new XingService())->get_task_type_show($old['type']);
                $mess .= ' - '.$old['name'];
                if($old['type'] == 1){
                    $enginname = BidModel::where(['id'=>$old['bidid']])->value('name');
                    $mess .= ' - '.$enginname;
                }
                if($old['type'] == 2){
                    $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                    $mess .= ' - '.$enginname;
                }
                $mess .= '（'.$param['explainclose'].'）';
                $zhierArr = explode(',', $old['zhier']);
                foreach($zhierArr as $uid){
                    (new WorkerService())->add_worker_task($uid, $mess, $old['id'], 'task');
                }


                (new OptService())->add_flow_log('关闭', 'task', $param['id'], '',$editStr);

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(4000,lang('code_4000'));
    }
    public function zhi_task(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = TaskModel::where(['id'=>$param['id']])->find();

            if(empty($param['startdt'])){
                return outputJson(4000, '请选择实际开始时间');
            }

            if($param['startdt'] > date("Y-m-d H:i:s")){
                return outputJson(4000, '实际开始时间不能大于当前时间');
            }
            if(!empty($param['enddt'])){
                if($param['enddt'] > date("Y-m-d H:i:s")){
                    return outputJson(4000, '实际完成时间不能大于当前时间');
                }
                if($param['startdt'] > $param['enddt']){
                    return outputJson(4000, '实际开始时间不能大于实际完成时间');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            $editStr = '';
            if($param['startdt'] != $old['startdt']){
                $editStr .= '实际开始时间修改为：'.$param['startdt'].'；';
            }
            if($param['enddt'] != $old['enddt']){
                $editStr .= '实际完成时间修改为：'.$param['enddt'].'；';
            }
            if($param['rate'] != $old['rate']){
                $editStr .= '完成比例修改为：'.$param['rate'].'%；';
            }



            if(!empty($param['enddt'])){
                $param['status'] = 5;
            }else{
                if(!empty($param['startdt'])){
                    $param['status'] = 3;
                }else{
                    $param['status'] = 1;
                }
            }






            Db::startTrans();
            try {
                TaskModel::update($param);

                if($param['startdt'] != $old['startdt'] || $param['enddt'] != $old['enddt']){
                    if($param['status'] == 3 || $param['status'] == 5){
                        $mess = '';
                        if($param['status'] == 3){
                            $mess .= '任务执行中：';
                        }
                        if($param['status'] == 5){
                            $mess .= '任务完成：';
                        }
                        //任务执行中：任务类型 - 任务标题 - 项目名称
                        $mess .= (new XingService())->get_task_type_show($old['type']);
                        $mess .= ' - '.$old['name'];
                        if($old['type'] == 1){
                            $enginname = BidModel::where(['id'=>$old['bidid']])->value('name');
                            $mess .= ' - '.$enginname;
                        }
                        if($old['type'] == 2){
                            $enginname = EnginModel::where(['id'=>$old['enginid']])->value('name');
                            $mess .= ' - '.$enginname;
                        }
                        (new WorkerService())->add_worker_task($old['created_user'], $mess, $old['id'], 'task');
                    }
                }


                (new OptService())->relation_file($files,'task',$param['id']);
                (new OptService())->add_flow_log('执行', 'task', $param['id'], '',$editStr);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TaskModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['files'] = (new OptService())->get_file('task', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


}
